<?php
/*************************************************************************************
 * geocoderquery.php
 * -----------------
 * phpGMapTrips 
 * 
 * This php application allows you to easily record your travel destinations 
 * and display them with Google maps.
 * 
 * Author: Stephane Coste (lescoste@gmail.com)
 * Copyright: (c) 2006 Stephane Coste (http://lescoste.net/)
 * Release Version: 0.1
 * CVS Revision Version: $Revision: 1.1 $
 * Date Started: 2006/02/19
 * Last Modified: $Date: 2006/08/01 16:09:22 $
 *
 *************************************************************************************
 *
 *     This file is part of phpGMapTrips.
 *
 *   phpGMapTrips is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   phpGMapTrips is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with phpGMapTrips; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ************************************************************************************/
?>
<?php
/**************************
fonctions de parsing du fichier xml
**************************/
$coordinates="";
$xmltagvalue="";

function gcStartElement($parser, $name, $attrs) {
}

function gcEndElement($parser, $name) {
	global $coordinates, $xmltagvalue;
	$xmltagvalue = trim($xmltagvalue);
	switch($name) {
		case 'COORDINATES':
			$coordinates = $xmltagvalue;
			break;
	}
	$xmltagvalue="";
}

function gcCharacterData($parser, $data) {
        global $xmltagvalue;
	$xmltagvalue .= $data;
}

class GeocoderCaller {
	var $gmapKey;
	var $address;
	var $long;
	var $lat;
function GeocoderCaller( $key, $address) {
	$this->gmapKey = $key;
	$this->address = $address;
}
function getLong() {
    return $this->long;
}
function getLat() {
    return $this->lat;
}

function callGoogle() {
	global $coordinates;

	// Desired address
	$addressUrl = "http://maps.google.com/maps/geo?q=".urlencode($this->address)."&output=xml&key=".$this->gmapKey;

	// Retrieve the URL contents
	$page = file_get_contents($addressUrl);

	$xml_parser = xml_parser_create($type);
	xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
	xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
	xml_set_element_handler($xml_parser, "gcStartElement", "gcEndElement");
	xml_set_character_data_handler($xml_parser, "gcCharacterData");
	if (!xml_parse($xml_parser, $page)) {
		die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
	}
	xml_parser_free($xml_parser);

	//echo "<pre>".$page."</pre><br/>";
	//echo "coord=".$coordinates."<br/>";

	// Parse the coordinate string
	list($longitude, $latitude, $altitude) = explode(",", $coordinates);

	$this->long = $longitude;
	$this->lat = $latitude;

	// Output the coordinates
	//echo "Longitude: $longitude, Latitude: $latitude";

}
}

?>


